﻿
using System;
using System.Linq;

namespace Patterns4Net.ArchitectureExplorer.Views
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    /// <summary>
    /// Interaction logic for DiagramView.xaml
    /// </summary>
    public partial class DiagramView : UserControl
    {
        public static DiagramView Instance;

        public static readonly DependencyProperty ActualHeightWrapperProperty =
            DependencyProperty.Register("ActualHeightWrapper", typeof(double), typeof(DiagramView));

        public static readonly DependencyProperty ActualWidthWrapperProperty =
            DependencyProperty.Register("ActualWidthWrapper", typeof(double), typeof(DiagramView));

        public DiagramView()
        {
            InitializeComponent();
            Instance = this;
            SetBinding(ActualWidthWrapperProperty, new Binding("Width") { Mode = BindingMode.OneWayToSource });
            SetBinding(ActualHeightWrapperProperty, new Binding("Height") { Mode = BindingMode.OneWayToSource });
            this.SizeChanged += OnSizeChanged;
            this.Graph.OnLayoutUpdated += OnGraphLayoutUpdated;            
        }

        private void OnGraphLayoutUpdated(object sender, EventArgs e)
        {

            if (this.Graph.Graph.Vertices.Any() ||
                this.Graph.Graph.SubGraphs.Any())
            {
                this.WelcomeMessage.Visibility = Visibility.Collapsed;
                this.ZoomControl.ZoomToFill();
            }
        }

        static void OnSizeChanged(object sender, RoutedEventArgs e)
        {
            var view = (DiagramView)sender;
            view.ActualHeightWrapper = view.ActualHeight;
            view.ActualWidthWrapper = view.ActualWidth;
        }

        public double ActualHeightWrapper
        {
            get { return (double)GetValue(ActualHeightWrapperProperty); }
            set { SetValue(ActualHeightWrapperProperty, value); }
        }

        public double ActualWidthWrapper
        {
            get { return (double)GetValue(ActualWidthWrapperProperty); }
            set { SetValue(ActualWidthWrapperProperty, value); }
        }
    }
}
